<?php
class PluginContentwedding_ModuleNews_MapperNews extends Mapper
{

    public function AddPage(PluginContentwedding_ModuleNews_EntityNews $oPage)
    {
        $sql = "INSERT INTO " . Config::Get('plugin.contentwedding.table.news') . "
            (news_pid,
            news_url,
            news_url_full,
            news_title,
            news_text,
            news_date_add,
            news_seo_keywords,
            news_seo_description,
            news_active,
            news_main,
            news_sort,
            news_auto_br
            )
            VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?d, ?d, ?d, ?d)
        ";
        if ($iId = $this->oDb->query(
            $sql, $oPage->getPid(), $oPage->getUrl(), $oPage->getUrlFull(), $oPage->getTitle(), $oPage->getText(),
            $oPage->getDateAdd(), $oPage->getSeoKeywords(), $oPage->getSeoDescription(), $oPage->getActive(),
            $oPage->getMain(), $oPage->getSort(), $oPage->getAutoBr())
        ) {
            return $iId;
        }
        return false;
    }

    public function UpdatePage(PluginContentwedding_ModuleNews_EntityNews $oPage)
    {
        $sql = "UPDATE " . Config::Get('plugin.contentwedding.table.news') . "
            SET news_pid = ? ,
            news_url = ? ,
            news_url_full = ? ,
            news_title = ? ,
            news_text = ? ,
            news_date_edit = ? ,
            news_seo_keywords = ? ,
            news_seo_description = ? ,
            news_active	 = ?,
            news_main	 = ?,
            news_sort	 = ?,
            news_auto_br	 = ?
            WHERE news_id = ?d
        ";
        if ($this->oDb->query($sql, $oPage->getPid(), $oPage->getUrl(), $oPage->getUrlFull(), $oPage->getTitle(),
            $oPage->getText(), $oPage->getDateEdit(), $oPage->getSeoKeywords(), $oPage->getSeoDescription(),
            $oPage->getActive(), $oPage->getMain(), $oPage->getSort(), $oPage->getAutoBr(), $oPage->getId())
        ) {
            return true;
        }
        return false;
    }

    public function SetPagesPidToNull()
    {
        $sql = "UPDATE " . Config::Get('plugin.contentwedding.table.news') . "
            SET
                news_pid = null,
                news_url_full = news_url
        ";
        if ($this->oDb->query($sql)) {
            return true;
        }
        return false;
    }

    public function GetPageByUrlFull($sUrlFull, $iActive)
    {
        $sql = "SELECT * FROM " . Config::Get('plugin.contentwedding.table.news')
            . " WHERE news_url_full = ? and news_active = ?d ";
        if ($aRow = $this->oDb->selectRow($sql, $sUrlFull, $iActive)) {
            return Engine::GetEntity('PluginContentwedding_News', $aRow);
        }
        return null;
    }

    public function GetPageById($sId)
    {
        $sql = "SELECT * FROM " . Config::Get('plugin.contentwedding.table.news') . " WHERE news_id = ? ";
        if ($aRow = $this->oDb->selectRow($sql, $sId)) {
            return Engine::GetEntity('PluginContentwedding_News', $aRow);
        }
        return null;
    }

    public function deletePageById($sId)
    {
        $sql = "DELETE FROM " . Config::Get('plugin.contentwedding.table.news') . " WHERE news_id = ? ";
        if ($aRow = $this->oDb->selectRow($sql, $sId)) {
            return true;
        }
        return false;
    }

    public function GetPages($aFilter)
    {
        $sPidNULL = '';
        if (array_key_exists('pid', $aFilter) and is_null($aFilter['pid'])) {
            $sPidNULL = 'and news_pid IS NULL';
        }
        $sql = "SELECT
                    *,
                    news_id as ARRAY_KEY,
                    news_pid as PARENT_KEY
                FROM
                    " . Config::Get('plugin.contentwedding.table.news') . "
                WHERE
                    1=1
                    { and news_active = ?d }
                    { and news_main = ?d }
                    { and news_pid = ? } {$sPidNULL}
                ORDER by news_sort desc;
                    ";
        if ($aRows = $this->oDb->select($sql,
            isset($aFilter['active']) ? $aFilter['active'] : DBSIMPLE_SKIP,
            isset($aFilter['main']) ? $aFilter['main'] : DBSIMPLE_SKIP,
            (array_key_exists('pid', $aFilter) and !is_null($aFilter['pid'])) ? $aFilter['pid'] : DBSIMPLE_SKIP
        )
        ) {
            return $aRows;
        }
        return null;
    }

    public function GetCountPage()
    {
        $sql = "SELECT count(*) as count FROM " . Config::Get('plugin.contentwedding.table.news') . " ";
        if ($aRow = $this->oDb->selectRow($sql)) {
            return $aRow['count'];
        }
        return null;
    }

    public function GetPagesByPid($sPid)
    {
        $sql = "SELECT * FROM " . Config::Get('plugin.contentwedding.table.news') . " WHERE news_pid = ? ";
        $aResult = array();
        if ($aRows = $this->oDb->select($sql, $sPid)) {
            foreach ($aRows as $aRow) {
                $aResult[] = Engine::GetEntity('PluginContentwedding_News', $aRow);
            }
        }
        return $aResult;
    }

    public function GetNextPageBySort($iSort, $sPid, $sWay)
    {
        if ($sWay == 'up') {
            $sWay = '>';
            $sOrder = 'asc';
        } else {
            $sWay = '<';
            $sOrder = 'desc';
        }
        $sPidNULL = '';
        if (is_null($sPid)) {
            $sPidNULL = 'news_pid IS NULL and';
        }
        $sql = "SELECT * FROM " . Config::Get('plugin.contentwedding.table.news')
            . " WHERE { news_pid = ? and } {$sPidNULL} news_sort {$sWay} ? order by news_sort {$sOrder} limit 0,1";
        if ($aRow = $this->oDb->selectRow($sql, is_null($sPid) ? DBSIMPLE_SKIP : $sPid, $iSort)) {
            return Engine::GetEntity('PluginContentwedding_News', $aRow);
        }
        return null;
    }

    public function GetMaxSortByPid($sPid)
    {
        $sPidNULL = '';
        if (is_null($sPid)) {
            $sPidNULL = 'and news_pid IS NULL';
        }
        $sql = "SELECT max(news_sort) as max_sort FROM " . Config::Get('plugin.contentwedding.table.news')
            . " WHERE 1=1 { and news_pid = ? } {$sPidNULL} ";
        if ($aRow = $this->oDb->selectRow($sql, is_null($sPid) ? DBSIMPLE_SKIP : $sPid)) {
            return $aRow['max_sort'];
        }
        return 0;
    }

    /**
     * List of active news
     *
     * @param integer $iCount
     * @param integer $iCurrPage
     * @param integer $iPerPage
     *
     * @return array
     */
    public function getListOfActivePages(&$iCount, $iCurrPage, $iPerPage)
    {
        $sql = 'SELECT `news`.* FROM `' . Config::Get('plugin.contentwedding.table.news') . '` AS `news`
            WHERE `news`.`news_active` = 1
            ORDER BY `news`.`news_id` ASC
            LIMIT ?d, ?d';
        $aPages = array();
        if ($aRows = $this->oDb->selectPage($iCount, $sql, ($iCurrPage - 1) * $iPerPage, $iPerPage)) {
            foreach ($aRows as $aPage) {
                $aPages[] = Engine::GetEntity('PluginContentwedding_News', $aPage);
            }
        }
        return $aPages;
    }

    /**
     * Count of active news
     *
     * @return integer
     */
    public function getCountOfActivePages()
    {
        $sql = 'SELECT COUNT(`news`.`news_id`)
            FROM `' . Config::Get('plugin.contentwedding.table.news') . '` AS `news`
            WHERE `news`.`news_active` = 1';
        return $this->oDb->selectCell($sql);
    }
}
?>